package com.ilearn.zookeeper;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;

/**
 * @Project: seb-core
 * @Description:
 * @Version 1.0
 * @Throws SystemException:
 * @Author: <li>2018/12/18 lyndon Create 1.0
 * @Copyright (c)2018-2018 中央结算公司，版权所有
 * @Modified By:
 */
public class ConnByCurator {
    public static void main(String[] args) throws Exception {
        final CuratorFramework curator = CuratorFrameworkFactory.builder()
                .connectString("192.168.100.100:2181")
                .sessionTimeoutMs(4000)
                .retryPolicy(new ExponentialBackoffRetry(1000, 3))
                .namespace("curator")
                .build();

        curator.start();

        // 创建节点
        // 这里创建的节点应该是 /curator/huzy/node1
        // 原生api中，必须是逐层创建节点，创建子节点的时候，父节点必须先创建，而这里则不需要
        curator.create()
                // 需要的话，创建其父节点
                .creatingParentsIfNeeded()
                // 创建持久化节点
                .withMode(CreateMode.PERSISTENT)
                .forPath("/mic/node1", "1".getBytes());

        // 删除节点
        //curator.delete().deletingChildrenIfNeeded().forPath("/mic/node1");

        //
        //final Stat stat = new Stat();
        //.getData().storingStatIn(stat).forPath("/mic/node1");

        // 修改节点
        //curator.setData().withVersion(stat.getVersion()).forPath("/mic/node1", "2".getBytes());

        // 关闭连接
        //curator.close();

    }
}
